layui.define(['jquery', 'table', 'form'], function (exports) {
    var $ = layui.jquery,
        table = layui.table,
        form = layui.form;

    $.article = {
        queryList: function () {
            var pageCurr;
            table.render({
                elem: '#test',
                url: path+'/api/showArticleList',
                toolbar: '#toolbarDemo',
                totalRow: true,
                cellMinWidth: 80,
                method: 'post', //默认：get请求
                page: true,
                cols: [[
                    {
                        type: 'checkbox',
                        fixed: 'left'
                    },
                    {
                        field: 'id',
                        title: 'ID',
                        fixed: 'left',
                        unresize: true,
                        sort: true,
                        totalRowText: '合计'
                    },
                    {
                        field: 'title',
                        title: '文章标题',
                        align: 'center',
                        templet:'#titleTpl'
                    },
                    {
                        field: 'summary',
                        title: '文章简介',
                        align: 'left'
                    },
                    {
                        field: 'traffic',
                        title: '访问量',
                        align: 'right',
                        sort: true,
                        totalRow: true
                    },
                    {
                        field: 'isTop',
                        title: '是否置顶',
                        align: 'center',
                        templet: function (d) {
                            if(d.isTop=='1'){
                                return '是';
                            }else {
                                return '否';
                            }
                        }
                    },
                    {
                        field: 'categoryName',
                        title: '所属分类',
                        align: 'center'
                    },
                    {
                        field: 'createBy',
                        title: '创建时间',
                        sort: true,
                        templet: function (d) {
                            return dateFtt('yyyy-MM-dd hh:mm', new Date(d.createBy));
                        }
                    },
                    {
                        field: 'modifiedBy',
                        title: '修改时间',
                        sort: true,
                        templet: function (d) {
                            return dateFtt('yyyy-MM-dd hh:mm', new Date(d.modifiedBy));
                        }
                    },
                    {
                        field: 'right',
                        title: '操作',
                        toolbar:"#barDemo"
                    }
                ]],
                done: function(res, curr, count){
                    pageCurr=curr;
                }
            });
        },
        listCategory:function(){
            $.post(
                path + '/api/showCategory',
                function (data) {
                    $("select[name=categoryId]").html('<option value="">请选择</option>');
                    $.each(data,function(index,element){
                        var html='<option value='+element.id+'>'+element.name+'</option>';
                        $("select[name=categoryId]").append(html);
                    });
                }
            );
        },
        edit:function(data){
            $('input[name=id]').val(data.id);
            $('input[name=title]').val(data.title);
            $('textarea[name=summary]').text(data.summary);
            $('select[name=categoryId]').val(data.categoryId);
            $('input:radio[name=isTop][value=0]').attr("checked",data.isTop==0?true:false);
            $('input:radio[name=isTop][value=1]').attr("checked",data.isTop==1?true:false);
            form.render();
        },
        toDetail:function(id){
            window.location.href = path+"/admin/articleDetail?articleId="+id;
        },
        cleanForm: function () {
            $('input[name=id]').val('');
            $('input[name=title]').val('');
            $('input[name=summary]').val('');
            $('select[name=categoryId]').val('');
            $('input:radio[name=isTop][value=0]').attr("checked",false);
            $('input:radio[name=isTop][value=1]').attr("checked",false);
            form.render();
        }
    };

    $(function () {
        $.article.queryList();
        $.article.listCategory();
    });

    /**
     * 导入对应的JS模块
     */
    exports('article', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});